﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Input;

namespace WPF.DazzleUI.Controls
{
    public class DazzleWindow:Window
    {
        
        public static readonly DependencyProperty DazzleWindowStateProperty = DependencyProperty.Register("DazzleWindowState", typeof(DazzleWindowState), typeof(DazzleWindow));


        public DazzleWindow()
        {
            this.DefaultStyleKey = typeof(DazzleWindow);

            // associate window commands with this instance
            this.CommandBindings.Add(new CommandBinding(Microsoft.Windows.Shell.SystemCommands.CloseWindowCommand, OnCloseWindow));
            this.CommandBindings.Add(new CommandBinding(Microsoft.Windows.Shell.SystemCommands.MaximizeWindowCommand, OnMaximizeWindow, OnCanResizeWindow));
            this.CommandBindings.Add(new CommandBinding(Microsoft.Windows.Shell.SystemCommands.MinimizeWindowCommand, OnMinimizeWindow, OnCanMinimizeWindow));
            this.CommandBindings.Add(new CommandBinding(Microsoft.Windows.Shell.SystemCommands.RestoreWindowCommand, OnRestoreWindow, OnCanResizeWindow));

        }

        protected override void OnInitialized(EventArgs e)
        {

            if (this.ResizeMode == System.Windows.ResizeMode.NoResize || this.ResizeMode == System.Windows.ResizeMode.CanMinimize)
            {
                DazzleWindowState = Controls.DazzleWindowState.MaxBtnCollapsed;
            }
            else
                DazzleWindowState = Controls.DazzleWindowState.Normal;
            base.OnInitialized(e);
        }


        /// <summary>
        /// Raises the System.Windows.Window.Closed event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
        }

        private void OnCanResizeWindow(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.ResizeMode == ResizeMode.CanResize || this.ResizeMode == ResizeMode.CanResizeWithGrip;
        }

        private void OnCanMinimizeWindow(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.ResizeMode != ResizeMode.NoResize;
        }

        private void OnCloseWindow(object target, ExecutedRoutedEventArgs e)
        {
            Microsoft.Windows.Shell.SystemCommands.CloseWindow(this);
        }

        private void OnMaximizeWindow(object target, ExecutedRoutedEventArgs e)
        {
            Microsoft.Windows.Shell.SystemCommands.MaximizeWindow(this);
        }

        private void OnMinimizeWindow(object target, ExecutedRoutedEventArgs e)
        {
            Microsoft.Windows.Shell.SystemCommands.MinimizeWindow(this);
        }

        private void OnRestoreWindow(object target, ExecutedRoutedEventArgs e)
        {
            Microsoft.Windows.Shell.SystemCommands.RestoreWindow(this);
        }

        public DazzleWindowState DazzleWindowState
        {
            get { return (DazzleWindowState)GetValue(DazzleWindowStateProperty); }
            private set { SetValue(DazzleWindowStateProperty, value); }
        }

        protected override void OnStateChanged(EventArgs e)
        {
            switch (this.WindowState)
            {
                case WindowState.Maximized:
                    if(this.ResizeMode == System.Windows.ResizeMode.NoResize || this.ResizeMode == System.Windows.ResizeMode.CanMinimize)
                        DazzleWindowState = Controls.DazzleWindowState.MaxBtnCollapsed;
                    else
                        DazzleWindowState = Controls.DazzleWindowState.Maximized;
                    break;
                case WindowState.Minimized:
                    DazzleWindowState = Controls.DazzleWindowState.Minimized;
                    break;
                case WindowState.Normal:
                    if (this.ResizeMode == System.Windows.ResizeMode.NoResize)
                        DazzleWindowState = Controls.DazzleWindowState.MaxBtnCollapsed;
                    else
                        DazzleWindowState = Controls.DazzleWindowState.Normal;
                    break;
                default:
                    break;
            }
            base.OnStateChanged(e);
        }
    }

    public enum DazzleWindowState
    {
        // 摘要:
        //     还原窗口。
        Normal = 0,
        //
        // 摘要:
        //     最小化窗口。
        Minimized = 1,
        //
        // 摘要:
        //     最大化窗口。
        Maximized = 2,

        /// <summary>
        /// 最大化按钮隐藏
        /// </summary>
        MaxBtnCollapsed = 3
    }
}
